package leetcode_cn;

/**
 * 最长回文子串
 *
 * @author briarbear
 * @blog https://briarbear.github.io
 * @date 2018-09-05 9:49
 */
public class LongestPalindrome {

    public String solution(String s){
        if (s==null || s.equals("")) return "";
        char[] array = s.toCharArray();
        int index = 1;
        int left,right,len = array.length;
        int max = 0;
        String res = s.substring(0,1);
        while (index<=len*2){
            left = (index-1)/2;     //关键点，设定步长为0.5
            right = (index+2)/2;
            while (left>=0&&right<len&&array[left] == array[right]){
                left--;
                right++;
            }
            if ((right-left-1) > max){
                res = s.substring(left+1,right);
                max = right-left-1;
            }
            index++;
        }

        return res;
    }


    public static void main(String[] args) {
//        System.out.println(new LongestPalindrome().solution("abb"));
//        System.out.println(new LongestPalindrome().solution("cbbd"));
        System.out.println(new LongestPalindrome().solution("cbbd"));
        
    }
}
